from PIL import Image
from django.db import models


class College(models.Model):
    """学院信息"""
    id = models.AutoField(primary_key=True, verbose_name="学院编号")
    name = models.CharField(max_length=32, unique=True, verbose_name='学院名称')
    info = models.CharField(max_length=255, verbose_name='学院简介')
    logo = models.ImageField(null=True)

    def save(self, *args, **kwargs):
        super(College, self).save()
        if self.logo:
            photo = Image.open(self.logo)
            photo.thumbnail((64, 64))
            photo.save(self.logo.path)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tb_college'
        verbose_name_plural = '学院'


class class_m(models.Model):
    """班级信息"""
    id = models.AutoField(primary_key=True, verbose_name="班级编号")
    name = models.CharField(max_length=32, unique=True, verbose_name='班级名称')
    college = models.ForeignKey(to=College, on_delete=models.PROTECT, db_column='collid', verbose_name='所属学院')
    teacher = models.CharField(max_length=32, verbose_name='班导')
    classroom = models.CharField(max_length=32, verbose_name='教室')
    info = models.CharField(max_length=255, verbose_name='班级介绍')
    photo = models.ImageField(null=True)

    class Meta:
        db_table = 'tb_class'
        verbose_name_plural = '班级'


class Student(models.Model):
    """学生信息"""
    id = models.AutoField(primary_key=True, verbose_name="学生编号")
    name = models.CharField(max_length=32, verbose_name='名称')
    class_m = models.ForeignKey(to=class_m, on_delete=models.PROTECT, db_column='classid', verbose_name='班级')
    college = models.ForeignKey(to=College, on_delete=models.PROTECT, db_column='collid', verbose_name='学院')
    age = models.SmallIntegerField(default=18, verbose_name='年龄')
    gender = models.BooleanField(default=True, verbose_name='性别')
    tel = models.CharField(max_length=16, default='', verbose_name='联系电话')
    addr = models.CharField(max_length=255, default='', verbose_name='家庭住址')

    class Meta:
        db_table = 'tb_student'
        verbose_name_plural = '学生'
